<template>
    <div class="view-main">
        <div class="view-header">
            <el-input placeholder="参数标题" v-model="queryParam.title" style="width: 200px"></el-input>
            <el-input placeholder="参数名称" v-model="queryParam.name" style="width: 200px" ></el-input>
            <span>
        <el-button type="primary" @click="getDataList()" icon="el-icon-search">查询</el-button>
                <el-button type="primary" @click="handleAdd" icon="el-icon-plus">添加</el-button>
      </span>
        </div>
        <div class="view-body">
            <div class="light-table">
                <el-table :data="tableData" height="300" highlight-current-row style="width: 100%">
                    <el-table-column property="title" label="参数标题" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column property="name" label="参数名称" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column property="param" label="参数值" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column property="remark" label="参数说明" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column label="操作" align="center" width="330">
                        <template slot-scope="scope">
                            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                            <el-button type="text" class="danger-color" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination background layout="total, sizes, prev, pager, next, jumper" :page-sizes="[10, 20, 50, 100]"
                               :total="total" :current-page="pageNum" :page-size.sync="pageSize" :page-count="pages"
                               @current-change="handlePageChange" @size-change="handleSizeChange"></el-pagination>
            </div>
        </div>
        <el-dialog class="no-bottom-padding user-dialog" :title="typeText" :visible.sync="dialogVisible" width="480px" :close-on-click-modal="false">
            <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="100px" size="mini" style="padding-right: 20px">
                <el-form-item label="参数标题" prop="title">
                    <el-input v-model="addForm.title" clearable style="width: 300px" :disabled="type === 1"></el-input>
                </el-form-item>

                <el-form-item label="参数名称" prop="name">
                    <el-input v-model="addForm.name" clearable style="width: 300px" :disabled="type === 1"></el-input>
                </el-form-item>

                <el-form-item label="参数值">
                    <el-input v-model="addForm.param" clearable style="width: 300px"></el-input>
                </el-form-item>

                <el-form-item label="参数说明">
                    <el-input v-model="addForm.remark" clearable style="width: 300px"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="type < 2">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateConfirm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { isEmpty, formatDate, stringFormat,getStartTimeOfDate, getEndTimeOfDate, getStartTimeOfWeek, getEndTimeOfWeek, getStartTimeOfMonth, getEndTimeOfMonth } from '../../utils/util'
    // import { checkLoginName, checkPhone, checkEmail, checkObj, isEmpty, stringFormat } from '../../utils/util'

    import { getAction } from '@/utils/manage'

    export default {
        name: 'param',
        data() {
            return {
                dialogVisible: false,
                type: 0,
                queryParam: {
                    title: '',
                    name: '',
                },
                addForm: {
                    title: '',
                    name: '',
                    param: '',
                    remark: '',
                },
                addRules: {
                    // trigger: 'blur'鼠标失去焦点时触发事件
                    title: [{ required: true, message: '请输入参数标题', trigger: 'blur' }],
                    name: [{ required: true, message: '请输入参数名称', trigger: 'blur' }],
                    param: [{ trigger: 'blur' }],
                    remark: [{ trigger: 'blur' }]
                },
                timeShow: false,
                // tableData: {},
                tableData: [],
                pageNum: 1,
                pageSize: 10,
                pages: 0,
                total: 0,
                url: {
                    page: '/sys/param',
                    insert: '/sys/param/insert',
                    update: '/sys/param/update/{0}',
                    delete: '/sys/param/delete/{0}',
                }
            }
        },
        created() {
            this.getDataList()
        },
        computed: {
            typeText() {
                return this.type === 0 ? '添加系统参数' : this.type === 1 ? '编辑系统参数' : '添加系统参数'
            }
        },

        methods: {
            handleAdd() {
                this.type = 0
                this.addForm = {
                    title: '',
                    name: '',
                    param: '',
                    remark: '',
                }
                this.dialogVisible = true
                if (!isEmpty(this.$refs.addForm)) {
                    this.$refs.addForm.resetFields()
                }
            },
            handleUpdate(row) {
                this.addForm = JSON.parse(JSON.stringify(row))
                this.type = 1
                this.dialogVisible = true
                if (!isEmpty(this.$refs.addForm)) {
                    this.$refs.addForm.resetFields()
                }
            },
            handleDelete(row) {
                this.$confirm('确认删除?', '提示', { type: 'warning' })
                    .then(() => {
                        if (!isEmpty(row) && !isEmpty(row.id)) {
                            var url = stringFormat(this.url.delete, [row.id])
                            console.log(this.url.delete)
                            this.$http.delete(url).then(() => {
                                this.getDataList()
                            })
                        }
                    })
                    .catch(() => {})
            },
            updateConfirm(){
                this.$refs.addForm.validate(valid => {
                    if (valid) {
                        let _form = JSON.parse(JSON.stringify(this.addForm))

                        /* insert */
                        if (this.type === 0) {
                            var address = this.url.insert
                            this.$http.put(address, _form).then(() => {
                                console.log("1111111")
                                console.log(this.addForm)
                                this.getDataList()
                            })
                            /* update */
                        } else {
                            var address = stringFormat(this.url.update, [_form.id])
                            console.log("---------")
                            console.log(_form)
                            this.$http.put(address, _form).then(() => {
                                this.getDataList()
                            })
                        }
                        this.dialogVisible = false
                    } else {
                        return false
                    }
                })
            },
            getDataList() {
                let params = { _pageSize: this.pageSize, _pageNum: this.pageNum, ...this.queryParam }

                // console.log("111")
                // console.log(params)
                this.$http.get(this.url.page, {params}).then(data => {
                    console.log(data)
                    let result = data.result
                    this.tableData = result.records
                    // console.log(this.tableData)
                    this.pageNum = result.current
                    this.pageSize = result.size
                    this.pages = result.pages
                    this.total = result.total
                })
            },
            handlePageChange(page) {
                this.pageNum = page
                this.getDataList()
            },
            handleSizeChange(size) {
                this.pageSize = size
                this.getDataList()
            }
        }
    }
</script>


<style lang="less" scoped>
    .view-header{
        margin-top: 20px;
    }
.dialog-footer{
    margin-right: 47px;
}
</style>